package org.hackystat.analysis.fft.util;

import java.util.logging.Logger;

import javax.xml.datatype.XMLGregorianCalendar;
import org.hackystat.sensorbase.client.SensorBaseClient;
import org.hackystat.sensorbase.resource.projects.jaxb.Project;
import org.hackystat.sensorbase.resource.projects.jaxb.ProjectSummary;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
import org.hackystat.utilities.logger.HackystatLogger;

public class MigrateData2Home {

  private static final String fromURL = "http://dasha.ics.hawaii.edu:9876/sensorbase";
  private static final String fromUser = "senin@hawaii.edu";
  private static final String fromPassword = "---";

  private static final String toURL = "http://localhost:9876/sensorbase";
  private static final String toUser = "senin@hawaii.edu";
  private static final String toPassword = "---";

  private static final String projectName = "Default";
  private static final String projectOwner = fromUser;
  private static final int chunkSize = 250;

  public static void main(String[] args) throws Exception {
    Logger logger = HackystatLogger.getLogger("migrate", "migrate");
    // SensorBaseClient.registerUser(toURL, toUser);
    SensorBaseClient toClient = new SensorBaseClient(toURL, toUser, toPassword);
    SensorBaseClient fromClient = new SensorBaseClient(fromURL, fromUser, fromPassword);

    logger.info("Getting Project definition.");
    Project prj = fromClient.getProject(projectOwner, projectName);

    logger.info("Getting Project summary");
    XMLGregorianCalendar startDay = prj.getStartTime();
    XMLGregorianCalendar endDay = prj.getEndTime();
    ProjectSummary ps = fromClient.getProjectSummary(prj.getOwner(), projectName, startDay, endDay);

    int totalDataEntries = ps.getSensorDataSummaries().getNumInstances().intValue();
    int numLoops = (totalDataEntries / chunkSize) + 1;
    logger.info("Total data: " + totalDataEntries + ", total loops " + numLoops);

    for (int i = 0; i < numLoops; i++) {
      int startIndex = i * chunkSize;

      long start = System.currentTimeMillis();
      SensorDataIndex index = fromClient.getProjectSensorData(prj.getOwner(), projectName,
          startDay, endDay, startIndex, chunkSize);
      long elapsedTime = System.currentTimeMillis() - start;
      logger.info("Retrieved index from " + startIndex + " in " + elapsedTime + " ms.");

      start = System.currentTimeMillis();
      SensorDatas bunch = new SensorDatas();
      for (SensorDataRef ref : index.getSensorDataRef()) {
        bunch.getSensorData().add(fromClient.getSensorData(ref));
      }
      elapsedTime = System.currentTimeMillis() - start;
      logger.info("Retrieved sensor data in: " + elapsedTime + " ms.");

      start = System.currentTimeMillis();
      toClient.putSensorDataBatch(bunch);
      elapsedTime = System.currentTimeMillis() - start;
      logger.info("Saved sensor data in: " + elapsedTime + " ms.");
    }
  }
}
